/**********************************************************
*
*   @file    useri2cReg.c
*   @author  Derrick Wang
*   @time    2018-4-26
*		@brief   i2c协议和外部交互的函数
*
*
***********************************************************/


#include "main.h"
#include "stm32f0xx_hal.h"
#include "userI2c.h"
#include "userI2cReg.h"
#include "userKey.h"
#include "userGame.h"

uint8_t cache[CACHE_SIZE]; 
uint8_t nowCmd;

struct userI2cRegStruct userI2cRegList[]=
{
	//通用寄存器 
	{REG_CHECK,1,CMD_RT_TRANSMIT},
	{REG_VERSION,1,CMD_RT_TRANSMIT},
	{REG_LED,1,CMD_RT_RECEIVE},
	{REG_RESET,0,CMD_RT_RECEIVE},
	//模块专用寄存器
	{REG_SOFT_VER,0,CMD_RT_RECEIVE},
	{REG_DRAW,0,CMD_RT_RECEIVE},
	{REG_MENU,0,CMD_RT_RECEIVE},
	{REG_GAME1,0,CMD_RT_RECEIVE},
	{REG_GAME2,0,CMD_RT_RECEIVE},
	{REG_GAME3,0,CMD_RT_RECEIVE},	
	{REG_SAVER,0,CMD_RT_RECEIVE},
	{REG_POPUP,15,CMD_RT_RECEIVE},
	
	{REG_UP,0,CMD_RT_RECEIVE},
	{REG_DOWN,0,CMD_RT_RECEIVE},	
	{REG_LEFT,0,CMD_RT_RECEIVE},
	{REG_RIGHT,0,CMD_RT_RECEIVE},
	{REG_START,0,CMD_RT_RECEIVE},
	{REG_RETURN,0,CMD_RT_RECEIVE},
	{REG_NUMBER,1,CMD_RT_RECEIVE},
	{REG_MENU_ADD_ITEM,15,CMD_RT_RECEIVE},  
	{REG_MENU_NOW_ITEM,1,CMD_RT_TRANSMIT},
	{REG_MENU_CLEAR_ITEM,1,CMD_RT_RECEIVE},

	{REG_LINE,0,CMD_RT_RECEIVE},
	{REG_VLINE,0,CMD_RT_RECEIVE},
	{REG_HLINE,0,CMD_RT_RECEIVE},
	{REG_RECT,0,CMD_RT_RECEIVE},
	{REG_FILL_RECT,0,CMD_RT_RECEIVE},
	{REG_FILL_SCREEN,0,CMD_RT_RECEIVE},
	{REG_CIRCLE,0,CMD_RT_RECEIVE},
	{REG_PART_CIRCLE,0,CMD_RT_RECEIVE},
	{REG_FILL_CIRCLE,0,CMD_RT_RECEIVE},
	{REG_FILL_PART_CIRCLE,0,CMD_RT_RECEIVE},
	{REG_TRIANGLE,0,CMD_RT_RECEIVE},
	{REG_FILL_TRIANGLE,0,CMD_RT_RECEIVE},
	{REG_ROUND_RECT,0,CMD_RT_RECEIVE},
	{REG_FILL_ROUND_RECT,0,CMD_RT_RECEIVE},
	{REG_BITMAP,0,CMD_RT_RECEIVE},
	{REG_PRINT,0,CMD_RT_RECEIVE},
	{REG_PRINTLN,0,CMD_RT_RECEIVE},
	{REG_DRAW_CHAR,0,CMD_RT_RECEIVE},
	{REG_SET_CURSOR,0,CMD_RT_RECEIVE},
	{REG_DRAW_PIXEL,0,CMD_RT_RECEIVE},
	{REG_CLEAR_SCREEN,0,CMD_RT_RECEIVE},
	{REG_BACKLIGHT,0,CMD_RT_RECEIVE},
	
};

//获取要发送的数据，返回-1为错误，其他值为返回的数据长度
//cmd为命令，buf用来存放获取到的数据
uint8_t user_I2cGetTransmitData(uint8_t reg,uint8_t *buf)
{
	switch(reg)
	{
		case REG_CHECK:
			buf[0]=REG_CHECK_REPLY;
			break;
		case REG_VERSION:
			buf[0]=VERSION;
			break;
		case REG_MENU_NOW_ITEM:
			buf[0]=gb.menuactiveItem;
			break;
	}
	return 1;
}
//命令执行函数
void user_I2cExecuteCallBack(uint8_t reg,uint8_t *buf,uint8_t size)
{
	switch(reg)
	{
		case REG_LED:
			if(buf[0]==0x00){
				LED_OFF();
			}
			else{
				LED_ON();
			}
			break;
		case REG_RESET:
			NVIC_SystemReset();
			break;
		case REG_SOFT_VER:
			gb.state=DIS_STATE_VER;			
			break;
		case REG_DRAW:
			gb.popupTimeLeft=0;   //进入绘图模式，就不再更新弹窗显示
			gb.display.clear();
			gb.display.persistence=true;   //保留每一次绘画内容
			gb.state=DIS_STATE_DRAW;
			break;
		case REG_MENU:
			gb.state=DIS_STATE_MENU;
			break;
		case REG_SAVER:
			gb.state=DIS_STATE_SAVER;
			break;
		case REG_GAME1:
			gb.state=DIS_STATE_GAME1;
			gb.gameParameter1=buf[0];
			break;
		case REG_GAME2:
			gb.state=DIS_STATE_GAME2;
			gb.gameParameter1=buf[0];
			break;
		case REG_GAME3:
			gb.state=DIS_STATE_GAME3;
			gb.gameParameter1=buf[0];
			break;
		case REG_POPUP:
			if(gb.state!=DIS_STATE_DRAW) //非绘图模式下才允许弹窗
			{
				if(buf[0]>13)buf[0]=13;
				memcpy(cache,buf+2,buf[0]);			
				cache[buf[0]]=NULL;   //添加字符串结尾符号 
				gb.popup(cache,buf[1]);
			}
			break;
		case REG_UP:
			nowCmd=CMD_UP;
			break;
		case REG_DOWN:
			nowCmd=CMD_DOWN;
			break;
		case REG_LEFT:
			nowCmd=CMD_LEFT;
			break;
		case REG_RIGHT:
			nowCmd=CMD_RIGHT;
			break;
		case REG_START:
			nowCmd=CMD_START;
			break;
		case REG_RETURN:
			nowCmd=CMD_RETURN;
			break;
		case REG_NUMBER:
			if(size==1)
			{
				cache[0]=buf[0];
				nowCmd=CMD_NUMBER;			
			}
			break;
		case REG_MENU_ADD_ITEM:
			if(buf[0]>13)buf[0]=13;
			if(gb.menuItemNumber<MENU_MAX_ITEM)
			{
				memcpy(*(gb.menuItemList+gb.menuItemNumber),buf+1,buf[0]);
				gb.menuItemNumber+=1;
				gb.menuactiveItem=0;
			}
			break;
		case REG_MENU_CLEAR_ITEM:
			gb.menuItemNumber=0;
			gb.menuactiveItem=0;
			break;
		case REG_LINE:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.drawLine(buf[0],buf[1],buf[2],buf[3]);
			break;
		case REG_VLINE:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.drawFastVLine(buf[0],buf[1],buf[2]);
			break;
		case REG_HLINE:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.drawFastHLine(buf[0],buf[1],buf[2]);
			break;
		case REG_RECT:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.drawRect(buf[0],buf[1],buf[2],buf[3]);
			break;
		case REG_FILL_RECT:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.fillRect(buf[0],buf[1],buf[2],buf[3]);
			break;
		case REG_FILL_SCREEN:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.fillScreen(BLACK);   //这个参数无关，都会填充位黑色
			break;
		case REG_CIRCLE:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.drawCircle(buf[0],buf[1],buf[2]);
			break;
		case REG_PART_CIRCLE:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.drawCircleHelper(buf[0],buf[1],buf[2],buf[3]);
			break;
		case REG_FILL_CIRCLE:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.fillCircle(buf[0],buf[1],buf[2]);
			break;
		case REG_FILL_PART_CIRCLE:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.fillCircleHelper(buf[0],buf[1],buf[2],buf[3],0);
			break;
		case REG_TRIANGLE:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.drawTriangle(buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);
			break;
		case REG_FILL_TRIANGLE:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.fillTriangle(buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]); 
			break;
		case REG_ROUND_RECT:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.drawRoundRect(buf[0],buf[1],buf[2],buf[3],buf[4]);
			break;
		case REG_FILL_ROUND_RECT:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.fillRoundRect(buf[0],buf[1],buf[2],buf[3],buf[4]);
			break;
		case REG_BITMAP:
			if(gb.state!=DIS_STATE_DRAW)break;
			if(buf[2]*buf[3]<=512)     //最大只能画64*8=512像素的图像
			{
				gb.display.drawBitmap(buf[0],buf[1],buf+2);
			}
			break;
		case REG_PRINT:
			if(gb.state!=DIS_STATE_DRAW)break;
			buf[buf[0]+1]=NULL;
			gb.display.print(buf+1);
			break;
		case REG_PRINTLN:
			if(gb.state!=DIS_STATE_DRAW)break;
			buf[buf[0]+1]=NULL;
			gb.display.println(buf+1); 
			break;
		case REG_DRAW_CHAR:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.drawChar(buf[0],buf[1],buf[2],buf[3]);
			break;		
		case REG_SET_CURSOR:
		    if(gb.state!=DIS_STATE_DRAW)break;	
			gb.display.setCursor(buf[0],buf[1]);
			break;
		case REG_DRAW_PIXEL:
			if(gb.state!=DIS_STATE_DRAW)break;	
			gb.display.drawPixel(buf[0],buf[1]);
			break;
		case REG_CLEAR_SCREEN:
			if(gb.state!=DIS_STATE_DRAW)break;
			gb.display.clear();
			break;
		case REG_BACKLIGHT:
			gb.display.setBackLight(buf[0]);
	}
}
//获取到当前接收到的i2c命令
uint8_t user_i2cGetCmd()
{
	uint8_t tmpCmd=nowCmd;
	nowCmd=0;
	return tmpCmd;
}

